package com.manxi.mapper;

import com.manxi.entity.ShoppingCart;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface ShoppingCartMapper {

    /**
     * 动态条件查询购物车
     *
     * @param shoppingCart
     * @return
     */
    @Select("<script> " +
            "select * from shopping_cart " +
            "where 1 = 1 " +
            "<if test = 'userId != null'>AND user_id = #{userId}</if> " +
            "<if test = 'dishId != null'>AND dish_id = #{dishId}</if> " +
            "<if test = 'setmealId != null'>AND setmeal_id = #{setmealId}</if> " +
            "<if test = 'dishFlavor != null'>AND dish_flavor = #{dishFlavor}</if> " +
            "order by create_time desc " +
            "</script>")
    List<ShoppingCart> list(ShoppingCart shoppingCart);

    /**
     * 根据购物车id更改购物车商品数量
     *
     * @param shoppingCart
     */
    @Update("update shopping_cart set number = #{number} where id = #{id}")
    void updateNumberById(ShoppingCart shoppingCart);

    /**
     * 插入一条购物车数据
     *
     * @param shoppingCart
     */
    @Insert("insert into shopping_cart (name,image,user_id,dish_id,setmeal_id,dish_flavor,number,amount,create_time)" +
            "values(#{name},#{image},#{userId},#{dishId},#{setmealId},#{dishFlavor},#{number},#{amount},#{createTime})")
    void insert(ShoppingCart shoppingCart);

    /**
     * 根据用户id清空购物车
     */
    @Delete("delete from shopping_cart where user_id = #{userId}")
    void deleteByUserId(Long userId);

    /**
     * 根据id删除商品
     *
     * @param id
     */
    @Delete("delete from shopping_cart where id = #{id}")
    void deleteById(Long id);

    /**
     * 批量添加到购物车
     *
     * @param shoppingCartList
     */
    @Insert("<script> " +
            "insert into shopping_cart (name,image,user_id,dish_id,setmeal_id,dish_flavor,number,amount,create_time) values " +
            "<foreach collection='shoppingCartList' item='sc' separator=','>" +
            "(#{sc.name},#{sc.image},#{sc.userId},#{sc.dishId},#{sc.setmealId},#{sc.dishFlavor},#{sc.number},#{sc.amount},#{sc.createTime}) " +
            "</foreach>" +
            "</script>")
    void insertBatch(List<ShoppingCart> shoppingCartList);

}
